package com.management.demo.controller.wx;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.demo.common.dto.PageDTO;
import com.management.demo.domain.wx.Banner;
import com.management.demo.dto.wx.BannerDTO;
import com.management.demo.dto.wx.BannerQueryDTO;
import com.management.demo.service.BannerService;
import com.management.demo.vo.wx.BannerVO;
import com.management.demo.common.vo.PageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "Banner图接口")
@RestController
@RequestMapping("wx/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @Operation(summary = "分页查询banner图")
    @PostMapping("page")
    public SaResult page(@RequestBody BannerQueryDTO bannerQueryDTO) {
        Integer status = bannerQueryDTO.getStatus();
        Integer type = bannerQueryDTO.getType();
        Integer currentPage = bannerQueryDTO.getCurrentPage();
        Integer pageSize = bannerQueryDTO.getPageSize();

        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<Banner>()
                .eq(status != null, Banner::getStatus, status)
                .eq(type != null, Banner::getType, type)
                .orderByDesc(Banner::getType).orderByAsc(Banner::getSort);
        Page<Banner> bannerPage = bannerService.page(new Page<>(currentPage, pageSize), queryWrapper);

        PageVO<BannerVO> bannerPageVO = PageDTO.getPageVO(bannerPage, item -> {
            BannerVO bannerVO = BeanUtil.copyProperties(item, BannerVO.class);
            bannerVO.setPosition(bannerVO.getType() == 1 ? "点餐页" : "首页");
            return bannerVO;
        });
        return SaResult.ok().setData(bannerPageVO);
    }

    @Operation(summary = "查询所有banner图")
    @PostMapping("list")
    public SaResult list(@RequestBody BannerQueryDTO bannerQueryDTO) {
        Integer status = bannerQueryDTO.getStatus();
        Integer type = bannerQueryDTO.getType();
        List<Banner> bannerList = bannerService.list(new LambdaQueryWrapper<Banner>()
                .eq(status != null, Banner::getStatus, status)
                .eq(type != null, Banner::getType, type)
                .orderByDesc(Banner::getType).orderByAsc(Banner::getSort));
        List<BannerVO> result = BeanUtil.copyToList(bannerList, BannerVO.class);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "新增/修改banner图信息")
    @PostMapping("createOrEdit")
    public SaResult createOrEdit(@RequestBody BannerDTO bannerDTO) {
        Banner banner = BeanUtil.copyProperties(bannerDTO, Banner.class);
        boolean result = bannerService.saveOrUpdate(banner);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "根据id获取banner图信息")
    @GetMapping("getById/{id}")
    public SaResult getById(@Parameter(description = "banner图id")
                            @PathVariable Integer id) {
        Banner banner = bannerService.getById(id);
        BannerVO result = BeanUtil.copyProperties(banner, BannerVO.class);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "删除banner图信息")
    @DeleteMapping("remove/{id}")
    public SaResult remove(@Parameter(description = "banner图id")
                           @PathVariable Integer id) {
        boolean result = bannerService.removeById(id);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "修改banner图展示状态")
    @PostMapping("changeStatus/{id}/{status}")
    public SaResult changeStatus(@Parameter(description = "banner图id") @PathVariable Integer id,
                                 @Parameter(description = "是否展示") @PathVariable Integer status) {
        UpdateWrapper<Banner> categoryUpdateWrapper = new UpdateWrapper<>();
        categoryUpdateWrapper.set("status", status);
        categoryUpdateWrapper.eq("id", id);
        boolean result = bannerService.update(categoryUpdateWrapper);
        return SaResult.ok().setData(result);
    }
}
